//
//  MainViewController.m
//  网易新闻
//
//  Created by Weefeng Ma on 16/7/9.
//  Copyright © 2016年 maweefeng. All rights reserved.
//
#define HMColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define HMColorRGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]

//定义 button 的宽度
#define button_width 60
#define margin 10
// 随机色
#define HMRandomColor HMColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))

#import "MainViewController.h"
#import "HMTitleView.h"
#import "HMLeftMenu.h"
#import "NavigationController.h"

@interface MainViewController ()<HMLeftMenuDelegate>
{ UIViewController *news;
}
/**
 
 
 *  正在显示的导航控制器
 */
@property (nonatomic, weak) NavigationController *showingNavigationController;
@property(nonatomic,strong)UIScrollView * scrollView ;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.创建子控制器
    // 1.1.新闻控制器
    news = [[UIViewController alloc] init];
    [self setupVc:news title:@"新闻"];
    //在控制器上的 scrollView会自动偏移 所以要把自动偏移关闭
    news.automaticallyAdjustsScrollViewInsets=NO;
    [self setupScrollView];
    

    
    // 1.2.订阅控制器
    UIViewController *reading = [[UIViewController alloc] init];
    [self setupVc:reading title:@"订阅"];
    
    // 1.3.图片控制器
    UIViewController *photo = [[UIViewController alloc] init];
    [self setupVc:photo title:@"图片"];
    
    // 1.4.视频控制器
    UIViewController *video = [[UIViewController alloc] init];
    [self setupVc:video title:@"视频"];
    
    // 1.5.跟帖控制器
    UIViewController *comment = [[UIViewController alloc] init];
    [self setupVc:comment title:@"跟帖"];
    
    // 1.6.电台控制器
    UIViewController *radio = [[UIViewController alloc] init];
    [self setupVc:radio title:@"电台"];
    
    // 2.添加左菜单
    HMLeftMenu *leftMenu = [[HMLeftMenu alloc] init];
    leftMenu.delegate = self;
    leftMenu.height = 300;
    leftMenu.width = 200;
    leftMenu.y = 60;
    
    [self.view insertSubview:leftMenu atIndex:1];



}
-(void)click{

    NSLog(@"2");
}
-(void)setupScrollView{
    
   
    UIScrollView * titleScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, 375, 30)];
    titleScrollView.contentSize=CGSizeMake(10*button_width, 30);
    titleScrollView.bounces=NO;
    titleScrollView.directionalLockEnabled=YES;
    titleScrollView.backgroundColor=[UIColor whiteColor];
    NSArray * arr=[NSArray arrayWithObjects:@"体育",@"国际",@"财经",@"国外",@"社会",@"娱乐",@"搞笑",@"理财",@"股票",@"社会",nil];
    for (NSUInteger i =0; i<10; i++) {
        
        UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
        button.frame=CGRectMake(i*button_width, 0, button_width, 30);
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        button.backgroundColor=[UIColor blackColor];
        button.titleLabel.textAlignment=NSTextAlignmentCenter;
        [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
        
        [titleScrollView addSubview:button];
    }
    [news.view addSubview:titleScrollView];



}
- (void)setupVc:(UIViewController *)vc title:(NSString *)title
{
    
    // 1.设置背景色
    vc.view.backgroundColor = HMRandomColor;
//    vc.view.backgroundColor=[UIColor whiteColor];

    // 2.设置标题
    HMTitleView *titleView = [[HMTitleView alloc] init];
    titleView.title = title;
    vc.navigationItem.titleView = titleView;
    
    // 3.设置左右按钮
    UIBarButtonItem * leftBtn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"top_navigation_menuicon"] style:UIBarButtonItemStylePlain target:self action:@selector(leftMenu)];
    UIBarButtonItem * rightBtn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"top_navigation_infoicon"] style:UIBarButtonItemStylePlain target:self action:@selector(rightMenu)];
    vc.navigationItem.rightBarButtonItem=rightBtn;
    vc.navigationItem.leftBarButtonItem=leftBtn;
    

    // 4.包装一个导航控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    // 让newsNav成为self（HMMainViewController）的子控制器，能保证：self在，newsNav就在
    // 如果两个控制器互为父子关系，那么它们的view也应该互为父子关系
    [self addChildViewController:nav];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
-(void)leftMenu{
    NSLog(@"leftMenu---");
    [UIView animateWithDuration:0.5 animations:^{
        // 取出正在显示的导航控制器的view
        UIView *showingView = self.showingNavigationController.view;
        //               // 缩放比例
        CGFloat navH = [UIScreen mainScreen].bounds.size.height - 2 * 60;
        CGFloat scale = navH / [UIScreen mainScreen].bounds.size.height;
        
        // 菜单左边的间距
        CGFloat leftMenuMargin = [UIScreen mainScreen].bounds.size.width * (1 - scale) * 0.5;
        CGFloat translateX = 200 - leftMenuMargin;
        
        CGFloat topMargin = [UIScreen mainScreen].bounds.size.height * (1 - scale) * 0.5;
        CGFloat translateY = topMargin - 60;
        
        // 缩放
        CGAffineTransform scaleForm = CGAffineTransformMakeScale(scale, scale);
        // 平移
        CGAffineTransform translateForm = CGAffineTransformTranslate(scaleForm, translateX / scale, -translateY / scale);
        
        showingView.transform = translateForm;
        
        // 添加一个遮盖
        UIButton *cover = [[UIButton alloc] init];
        [cover addTarget:self action:@selector(coverClick:) forControlEvents:UIControlEventTouchUpInside];
        cover.frame = showingView.bounds;
        [showingView addSubview:cover];
    }];


}
-(void)rightMenu{

}
-(void)viewWillAppear:(BOOL)animated{
//    self.navigationController.navigationBarHidden=NO;

}


- (void)coverClick:(UIButton *)cover
{
    [UIView animateWithDuration:0.5 animations:^{
        self.showingNavigationController.view.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [cover removeFromSuperview];
        NSLog(@"11");
    }];
}
#pragma mark - HMLeftMenuDelegate
- (void)leftMenu:(HMLeftMenu *)menu didSelectedButtonFromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex
{
    // 0.移除旧控制器的view
    NavigationController *oldNav = self.childViewControllers[fromIndex];
//    NSLog(@"-----%lu",(unsigned long)fromIndex);
    [oldNav.view removeFromSuperview];
    
    // 1.显示新控制器的view
    NavigationController *newNav = self.childViewControllers[toIndex];
//     NSLog(@"-----%lu",(unsigned long)toIndex);
    // 设置新控制的transform跟旧控制器一样
    newNav.view.transform = oldNav.view.transform;
    // 设置阴影
    newNav.view.layer.shadowColor = [UIColor blackColor].CGColor;
    newNav.view.layer.shadowOffset = CGSizeMake(-3, 0);
    newNav.view.layer.shadowOpacity = 0.2;
    [self.view addSubview:newNav.view];
    
    // 2.设置当前正在显示的控制器
    self.showingNavigationController = newNav;
    
    // 3.清空transform
    [UIView animateWithDuration:0.5 animations:^{
        newNav.view.transform = CGAffineTransformIdentity;
    }];
}

@end
